package de.haw.shc.Config;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.larswerkman.colorpicker.*;
import de.haw.shc.Action.Action_Impl;
import de.haw.shc.Interfaces.DeviceProperty.Device;
import de.haw.shc.JSONObjects.Blinds;
import de.haw.shc.JSONObjects.Curtain;
import de.haw.shc.JSONObjects.Light;
import de.haw.shc.JSONObjects.Window;
import de.haw.shc.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: mizus
 * Date: 22.06.13
 * Time: 15:48
 * To change this template use File | Settings | File Templates.
 */

public class SHC_List_Adapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;
    private List<Device> tEntryList;
    private ColorPicker picker;

    public SHC_List_Adapter(Context context, List<Device> aList){
        inflater = LayoutInflater.from(context);
        tEntryList = aList;
    }

    @Override
    public int getGroupCount() {
        return tEntryList.size();  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Object getGroup(int groupPosition) {
        return tEntryList.get(groupPosition);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean hasStableIds() {
        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView == null)
            convertView = inflater.inflate(R.layout.parent_list_item, null);

        TextView textView = (TextView) convertView.findViewById(R.id.list_parent_item_title_view);
        textView.setText(((Device)getGroup(groupPosition)).getViewName());

        final int  position = groupPosition;
        final ToggleButton toggleButton = (ToggleButton) convertView.findViewById(R.id.list_parent_btn_title_view);
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               action((Device)getGroup(position),  toggleButton); //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        return convertView;  //To change body of implemented methods use File | Settings | File Templates.
    }


    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            final int position = groupPosition;


            if(((Device)getGroup(groupPosition)).isColorPicker()) {
                convertView = inflater.inflate(R.layout.color_act, null);
                picker = (ColorPicker) convertView.findViewById(R.id.picker);
                SVBar svBar = (SVBar) convertView.findViewById(R.id.svbar);
                OpacityBar opacityBar = (OpacityBar) convertView.findViewById(R.id.opacitybar);
                SaturationBar saturationBar = (SaturationBar) convertView.findViewById(R.id.saturationbar);
                ValueBar valueBar = (ValueBar) convertView.findViewById(R.id.valuebar);

                picker.addSVBar(svBar);
                picker.addOpacityBar(opacityBar);
                picker.addSaturationBar(saturationBar);
                picker.addValueBar(valueBar);
                Button color_btn = (Button)convertView.findViewById(R.id.btn_set_color);


                final ToggleButton parentBtn = (ToggleButton)parent.getChildAt(groupPosition).findViewById(R.id.list_parent_btn_title_view);

                color_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                       parentBtn.setSelected(true);
                       parentBtn.setChecked(true);
                       colorAction(position); //To change body of implemented methods use File | Settings | File Templates.
                    }
                });

            } else {
                convertView = inflater.inflate(R.layout.child_list_slider_item, null);
                final SeekBar slider = (SeekBar)convertView.findViewById(R.id.seekBar);
                Button silderbtn = (Button) convertView.findViewById(R.id.btn_silder_action);



                final ToggleButton parentBtn = (ToggleButton)parent.getChildAt(groupPosition).findViewById(R.id.list_parent_btn_title_view);

                silderbtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        parentBtn.setSelected(true);
                        parentBtn.setChecked(true);
                        silderAction(slider, position);
                    }
                });
            }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }



    void action(Device device, ToggleButton btn){
        //Close
        if(!btn.isChecked()){
            if(device.isBlind()){
                Blinds blind = (Blinds)device;
                Action_Impl.getInstance().iniAction(blind.createCloseBlinds(), blind.getDeviceControll());
                Toast.makeText(inflater.getContext(),  R.string.toast_close, Toast.LENGTH_SHORT).show();
            }else if (device.isLight()){
                Light light = (Light)device;
                Action_Impl.getInstance().iniAction(light.createLightOff(), light.getDeviceControll());
                Action_Impl.getInstance().iniAction(light.createColorLight(0,0,0,0), light.getDeviceControll());
                Toast.makeText(inflater.getContext(),  R.string.toast_off, Toast.LENGTH_SHORT).show();
            }else if (device.isRadiator()){
                Curtain curtain = (Curtain)device;
                Action_Impl.getInstance().iniAction(curtain.createCloseCurtain(), curtain.getDeviceControll());
                Toast.makeText(inflater.getContext(),  R.string.toast_close, Toast.LENGTH_SHORT).show();
            } else if (device.isWindow()){
                Window window = (Window)device;
                Action_Impl.getInstance().iniAction(window.createCloseWindowObject(), window.getDeviceControll());
                Toast.makeText(inflater.getContext(),  R.string.toast_close, Toast.LENGTH_SHORT).show();
            } else if (device.isCurtain()){
                Curtain curtain = (Curtain)device;
                Action_Impl.getInstance().iniAction(curtain.createCloseCurtain(), curtain.getDeviceControll());
                Toast.makeText(inflater.getContext(), R.string.toast_close, Toast.LENGTH_SHORT).show();
            }

            //Open/On
        } else{
            if(device.isBlind()){
                Blinds blind = (Blinds)device;
                Action_Impl.getInstance().iniAction(blind.createOpenBlinds(), blind.getDeviceControll());
                Toast.makeText(inflater.getContext(), R.string.toast_open, Toast.LENGTH_SHORT).show();
            }else if (device.isLight()){
                Light light = (Light)device;
                if (light.getName().equals("bathroom_light_"))
                    Action_Impl.getInstance().iniAction(light.createColorLight(255,255,255,0), light.getDeviceControll());
                else
                    Action_Impl.getInstance().iniAction(light.createLightOn(), light.getDeviceControll());
                Toast.makeText(inflater.getContext(), R.string.toast_on, Toast.LENGTH_SHORT).show();
            }else if (device.isRadiator()){
                Curtain curtain = (Curtain)device;
                Action_Impl.getInstance().iniAction(curtain.createOpenCurtain(), curtain.getDeviceControll());
                Toast.makeText(inflater.getContext(), R.string.toast_open, Toast.LENGTH_SHORT).show();
            } else if (device.isWindow()){
                Window window = (Window)device;
                Action_Impl.getInstance().iniAction(window.createOpenWindowObject(), window.getDeviceControll());
                Toast.makeText(inflater.getContext(), R.string.toast_open, Toast.LENGTH_SHORT).show();
            } else if (device.isCurtain()){
                Curtain curtain =(Curtain)device;
                Action_Impl.getInstance().iniAction(curtain.createOpenCurtain(), curtain.getDeviceControll());
                Toast.makeText(inflater.getContext(), R.string.toast_open, Toast.LENGTH_SHORT).show();
            }
        }
        Log.d("Action Normal", device.toString());
    }

    void colorAction( int pos){

        int color  = picker.getColor();
        picker.setOldCenterColor(color);
        int blue =  Color.blue(color);
        int green = Color.green(color);
        int red = Color.red(color);

        Device device = (Device)getGroup(pos);
        Light light = (Light)device;

        Action_Impl.getInstance().iniAction(light.createColorLight(red, green, blue, 0), light.getDeviceControll());

        Log.d("Farbe setzen", light.toString());
    }

    void silderAction(SeekBar seekBar, int position){
        int pos = seekBar.getProgress();
        int gard;
        if (pos <16)   gard = 0;
        else if (pos>= 16 && pos <36) gard = 5;
        else if (pos >= 36 && pos <66) gard = 10;
        else if (pos >= 66 && pos <86) gard = 15;
        else gard = 20;

        Device device = (Device) getGroup((position));
        if (device.isWindow())   {
            Window window = (Window)device;
            Action_Impl.getInstance().iniAction(window.createSelfSettingsObject(gard), window.getDeviceControll());
        }  else if (device.isCurtain()){
            Curtain curtain = (Curtain)device;
            //ToDo Action Self
            Toast.makeText(inflater.getContext(), "coming soon", Toast.LENGTH_SHORT).show();

        }   else if (device.isBlind()){
            Blinds blinds = (Blinds)device;
            if(gard >= 5 && gard <= 15)
                Action_Impl.getInstance().iniAction(blinds.createhalfOpenBlinds(), blinds.getDeviceControll());
            else if( gard < 5)
                Action_Impl.getInstance().iniAction(blinds.createCloseBlinds(), blinds.getDeviceControll());
            else if(gard > 15)
                Action_Impl.getInstance().iniAction(blinds.createOpenBlinds(), blinds.getDeviceControll());
        }
        Log.d("Manuelle Einstellungen", device.toString());
    }

    private void out(String str){
        System.err.println(str);
    }



}
